package cn.zymall.goods.service.impl;

import cn.zymall.common.model.Kinds;
import cn.zymall.common.model.SpuAttr;
import cn.zymall.common.model.SpuBaseAttr;
import cn.zymall.goods.mapper.KindsMapper;
import cn.zymall.goods.mapper.SpuAttrMapper;
import cn.zymall.goods.mapper.SpuBaseAttrMapper;
import cn.zymall.goods.service.KindsService;
import cn.zymall.goods.service.SpuAttrService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author lxh
 * @Description 商品属性业务层接口实现类
 * @date 2020/8/18 9:52
 * @Classname KindsServiceImpl
 **/
@Service
public class SpuAttrServiceImpl extends ServiceImpl<SpuAttrMapper, SpuAttr> implements SpuAttrService {

	@Autowired
	private SpuAttrMapper spuAttrMapper;

	@Autowired
	SpuBaseAttrMapper spuBaseAttrMapper;

	@Override
	public List<SpuAttr> findById(Integer id) {
		return spuAttrMapper.findById(id);
	}

	@Override
	public void add(SpuAttr spuAttr) {
		spuAttrMapper.add(spuAttr);
	}

	@Override
	public void amend(SpuAttr spuAttr) {
		spuAttrMapper.amend(spuAttr);
	}

	@Override
	public void del(SpuAttr spuAttr) {
		spuAttrMapper.del(spuAttr);
	}

	@Override
	public List<SpuAttr> findAllById(Integer id) {
		return spuAttrMapper.findAllById(id);
	}

	@Override
	public SpuAttr findSpuAttrById(Integer id) {
		return spuAttrMapper.findSpuAttrById(id);
	}

	@Override
	public SpuAttr addBaseAttr(SpuAttr spuAttr) {
		spuAttrMapper.addBaseAttr(spuAttr);
		return spuAttr;
	}

	@Override
	public void delAttr(Integer id) {
		spuAttrMapper.delAttr(id);
	}

	@Override
	public List<SpuAttr> findByKindsId(Integer kindsId,Integer spuId) {
		List<SpuAttr> byKindsId = spuAttrMapper.findByKindsId(kindsId);
		for (int i = 0; i < byKindsId.size(); i++) {
			SpuBaseAttr spuBaseAttr=new SpuBaseAttr();
			spuBaseAttr.setSpuAttrId(byKindsId.get(i).getAttrId());
			spuBaseAttr.setSpuId(spuId);
			byKindsId.get(i).setSpuBaseAttrs(spuBaseAttrMapper.findById(spuBaseAttr));
		}
		return byKindsId;
	}
}
